﻿namespace Chain.IDAL
{
    using Chain.DBUtility;
    using Chain.Model;
    using System;
    using System.Data;
    using System.Data.SqlClient;
    using System.Runtime.InteropServices;
    using System.Text;

    public class Promotions
    {
        public int Add(Chain.Model.Promotions model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("insert into Promotions(");
            builder.Append("PromotionsTitle,PromotionsStart,PromotionsEnd,PromotionsMemLevel,PromotionsType,PromotionsTime)");
            builder.Append(" values (");
            builder.Append("@PromotionsTitle,@PromotionsStart,@PromotionsEnd,@PromotionsMemLevel,@PromotionsType,@PromotionsTime)");
            builder.Append(";select @@IDENTITY");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@PromotionsTitle", SqlDbType.NVarChar, 0x3e8), new SqlParameter("@PromotionsStart", SqlDbType.DateTime), new SqlParameter("@PromotionsEnd", SqlDbType.DateTime), new SqlParameter("@PromotionsMemLevel", SqlDbType.Int, 4), new SqlParameter("@PromotionsType", SqlDbType.Int, 4), new SqlParameter("@PromotionsTime", SqlDbType.DateTime) };
            cmdParms[0].Value = model.PromotionsTitle;
            cmdParms[1].Value = model.PromotionsStart;
            cmdParms[2].Value = model.PromotionsEnd;
            cmdParms[3].Value = model.PromotionsMemLevel;
            cmdParms[4].Value = model.PromotionsType;
            cmdParms[5].Value = model.PromotionsTime;
            object single = DbHelperSQL.GetSingle(builder.ToString(), cmdParms);
            if (single == null)
            {
                return 0;
            }
            return Convert.ToInt32(single);
        }

        public bool Delete(int PromotionsID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from Promotions ");
            builder.Append(" where PromotionsID=@PromotionsID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@PromotionsID", SqlDbType.Int, 4) };
            cmdParms[0].Value = PromotionsID;
            return (DbHelperSQL.ExecuteSql(builder.ToString(), cmdParms) > 0);
        }

        public bool DeleteList(string PromotionsIDlist)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from Promotions ");
            builder.Append(" where PromotionsID in (" + PromotionsIDlist + ")  ");
            return (DbHelperSQL.ExecuteSql(builder.ToString()) > 0);
        }

        public bool Exists(int PromotionsID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(1) from Promotions");
            builder.Append(" where PromotionsID=@PromotionsID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@PromotionsID", SqlDbType.Int, 4) };
            cmdParms[0].Value = PromotionsID;
            return DbHelperSQL.Exists(builder.ToString(), cmdParms);
        }

        public DataSet GetList(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select PromotionsID,PromotionsTitle,PromotionsStart,PromotionsEnd,PromotionsMemLevel,PromotionsType,PromotionsTime ");
            builder.Append(" FROM Promotions ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select ");
            if (Top > 0)
            {
                builder.Append(" top " + Top.ToString());
            }
            builder.Append(" PromotionsID,PromotionsTitle,PromotionsStart,PromotionsEnd,PromotionsMemLevel,PromotionsType,PromotionsTime ");
            builder.Append(" FROM Promotions ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            builder.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("SELECT * FROM ( ");
            builder.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                builder.Append("order by T." + orderby);
            }
            else
            {
                builder.Append("order by T.PromotionsID desc");
            }
            builder.Append(")AS Row, T.*  from Promotions T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                builder.Append(" WHERE " + strWhere);
            }
            builder.Append(" ) TT");
            builder.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(builder.ToString());
        }

        public Chain.Model.Promotions GetModel(int PromotionsID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select  top 1 PromotionsID,PromotionsTitle,PromotionsStart,PromotionsEnd,PromotionsMemLevel,PromotionsType,PromotionsTime from Promotions ");
            builder.Append(" where PromotionsID=@PromotionsID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@PromotionsID", SqlDbType.Int, 4) };
            cmdParms[0].Value = PromotionsID;
            Chain.Model.Promotions promotions = new Chain.Model.Promotions();
            DataSet set = DbHelperSQL.Query(builder.ToString(), cmdParms);
            if (set.Tables[0].Rows.Count <= 0)
            {
                return null;
            }
            if ((set.Tables[0].Rows[0]["PromotionsID"] != null) && (set.Tables[0].Rows[0]["PromotionsID"].ToString() != ""))
            {
                promotions.PromotionsID = int.Parse(set.Tables[0].Rows[0]["PromotionsID"].ToString());
            }
            if ((set.Tables[0].Rows[0]["PromotionsTitle"] != null) && (set.Tables[0].Rows[0]["PromotionsTitle"].ToString() != ""))
            {
                promotions.PromotionsTitle = set.Tables[0].Rows[0]["PromotionsTitle"].ToString();
            }
            if ((set.Tables[0].Rows[0]["PromotionsStart"] != null) && (set.Tables[0].Rows[0]["PromotionsStart"].ToString() != ""))
            {
                promotions.PromotionsStart = DateTime.Parse(set.Tables[0].Rows[0]["PromotionsStart"].ToString());
            }
            if ((set.Tables[0].Rows[0]["PromotionsEnd"] != null) && (set.Tables[0].Rows[0]["PromotionsEnd"].ToString() != ""))
            {
                promotions.PromotionsEnd = DateTime.Parse(set.Tables[0].Rows[0]["PromotionsEnd"].ToString());
            }
            if ((set.Tables[0].Rows[0]["PromotionsMemLevel"] != null) && (set.Tables[0].Rows[0]["PromotionsMemLevel"].ToString() != ""))
            {
                promotions.PromotionsMemLevel = int.Parse(set.Tables[0].Rows[0]["PromotionsMemLevel"].ToString());
            }
            if ((set.Tables[0].Rows[0]["PromotionsType"] != null) && (set.Tables[0].Rows[0]["PromotionsType"].ToString() != ""))
            {
                promotions.PromotionsType = int.Parse(set.Tables[0].Rows[0]["PromotionsType"].ToString());
            }
            if ((set.Tables[0].Rows[0]["PromotionsTime"] != null) && (set.Tables[0].Rows[0]["PromotionsTime"].ToString() != ""))
            {
                promotions.PromotionsTime = DateTime.Parse(set.Tables[0].Rows[0]["PromotionsTime"].ToString());
            }
            return promotions;
        }

        public DataSet GetPromotionsInfo(int PageSize, int PageIndex, out int resCount, params string[] strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select * from ");
            builder.Append("( ");
            builder.Append("select row_number() over(order by PromotionsID desc) as row,* from Promotions left join MemLevel on Promotions.PromotionsMemLevel=MemLevel.LevelID ");
            builder.Append(") as T ");
            builder.AppendFormat("where T.row>={0} and T.row<{1}", (PageSize * (PageIndex - 1)) + 1, (PageSize * PageIndex) + 1);
            DataSet set = DbHelperSQL.Query(builder.ToString());
            resCount = set.Tables[0].Rows.Count;
            return set;
        }

        public int GetRecordCount(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(1) FROM Promotions ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            object single = DbHelperSQL.GetSingle(builder.ToString());
            if (single == null)
            {
                return 0;
            }
            return Convert.ToInt32(single);
        }

        public bool Update(Chain.Model.Promotions model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("update Promotions set ");
            builder.Append("PromotionsTitle=@PromotionsTitle,");
            builder.Append("PromotionsStart=@PromotionsStart,");
            builder.Append("PromotionsEnd=@PromotionsEnd,");
            builder.Append("PromotionsMemLevel=@PromotionsMemLevel,");
            builder.Append("PromotionsType=@PromotionsType,");
            builder.Append("PromotionsTime=@PromotionsTime");
            builder.Append(" where PromotionsID=@PromotionsID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@PromotionsTitle", SqlDbType.NVarChar, 0x3e8), new SqlParameter("@PromotionsStart", SqlDbType.DateTime), new SqlParameter("@PromotionsEnd", SqlDbType.DateTime), new SqlParameter("@PromotionsMemLevel", SqlDbType.Int, 4), new SqlParameter("@PromotionsType", SqlDbType.Int, 4), new SqlParameter("@PromotionsTime", SqlDbType.DateTime), new SqlParameter("@PromotionsID", SqlDbType.Int, 4) };
            cmdParms[0].Value = model.PromotionsTitle;
            cmdParms[1].Value = model.PromotionsStart;
            cmdParms[2].Value = model.PromotionsEnd;
            cmdParms[3].Value = model.PromotionsMemLevel;
            cmdParms[4].Value = model.PromotionsType;
            cmdParms[5].Value = model.PromotionsTime;
            cmdParms[6].Value = model.PromotionsID;
            return (DbHelperSQL.ExecuteSql(builder.ToString(), cmdParms) > 0);
        }
    }
}

